package com.lc.yygh.user.controller.api;

import com.lc.yygh.common.result.Result;
import com.lc.yygh.common.utils.AuthContextHolder;
import com.lc.yygh.model.user.Patient;
import com.lc.yygh.user.service.PatientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

//就诊人管理接口
@RestController
@RequestMapping("/api/user/patient")
@Api(tags = "就诊人管理")
public class PatientApiController {
    @Autowired
    private PatientService patientService;

    //获取就诊人列表
    @GetMapping("auth/findAll")
    public Result findAll(HttpServletRequest request) {
        //获取当前登录用户id
        Long userId = AuthContextHolder.getUserId(request);
        //获取当前用户下的就诊人列表
        List<Patient> list = patientService.findAllByUserId(userId);
        return Result.success(list);
    }

    //添加就诊人
    @PostMapping("auth/save")
    public Result savePatient(@RequestBody Patient patient, HttpServletRequest request) {
        //获取当前登录用户id
        Long userId = AuthContextHolder.getUserId(request);
        patient.setUserId(userId);//就诊人就是当前用户
        patientService.save(patient);
        return Result.success();
    }
    //根据id获取就诊人信息
    @GetMapping("auth/get/{id}")
    public Result getPatient(@PathVariable Long id) {
        Patient patient = patientService.getPatientById(id);
        return Result.success(patient);
    }

    //获取就诊人信息(在预约下单那块用到)和上边这个方法要注意区分
    @GetMapping("inner/get/{id}")//inner意味着服务内部调用(远程调用)
    @ApiOperation(value = "获取就诊人")
    public Patient getPatientById(
            @ApiParam(name = "id", value = "就诊人id", required = true)
            @PathVariable("id") Long id) {
        return patientService.getPatientById(id);
    }

    //修改就诊人
    @PutMapping("auth/update")
    public Result updatePatient(@RequestBody Patient patient) {
        patientService.updateById(patient);
        return Result.success();
    }
    //删除就诊人
    @DeleteMapping("auth/remove/{id}")
    public Result removePatient(@PathVariable Long id) {
        patientService.removeById(id);
        return Result.success();
    }


}
